<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 14-3-5
 * Time: 下午12:04
 * To change this template use File | Settings | File Templates.
 */

class financeAction extends SaleAction
{
    public function income()
    {
        Load('extend');

        $finance="finance";
        $page = I("get.p");
        $query_keywords = I("get.query_keywords") ;
        if($query_keywords=="0")
            $query_keywords='';
        if (!mb_check_encoding($query_keywords, 'utf-8')){
            $query_keywords = iconv('gb2312', 'utf-8', $query_keywords);
        }
        $this->query_keywords=$query_keywords;
        $condition["ordernumber"]=  array("exp", "like '%$query_keywords%'");
        $condition["type"]= 1;
        $data = $this->getPageData($finance, $page, $condition);

        $this->assign('income_list', $data["list"]); // 数据列表变量赋值
        $this->assign('page_method', $data['pageshow']); //分页列表变量赋值
        $this->display(); // 输出模板
    }

    public function expend()
    {
        Load('extend');

        $finance="finance";
        $page = I("get.p");
        $query_keywords = I("get.query_keywords") ;
        if($query_keywords=="0")
            $query_keywords='';
        if (!mb_check_encoding($query_keywords, 'utf-8')){
            $query_keywords = iconv('gb2312', 'utf-8', $query_keywords);
        }
        $this->query_keywords=$query_keywords;
        $condition["cname"]=  array("exp", "like '%$query_keywords%'");
        $condition["type"]= 2;
        $data = $this->getPageData($finance, $page, $condition);

        $con_feeType["type"]=2;
        $feetypedata = $this->CacheArray("feetype", 'id',$con_feeType);
        $this->feetypeData=$feetypedata;



        $this->assign('expend_list', $data["list"]); // 数据列表变量赋值
        $this->assign('page_method', $data['pageshow']); //分页列表变量赋值
        $this->display(); // 输出模板
    }

    public function searchorder()
    {
        Load('extend');
        $query_ordernumber = I("get.query_ordernumber");
        $condition["ordernumber"]=  array("exp", "like '%$query_ordernumber%'");
        $table=M('agreement');
        $data=$table->where($condition)->select();

        $this->order_list=$data;
        $this->display();
    }

    public function addincome()
    {
        $feeTypeTable=M("feetype");
        $feeTypeData=$feeTypeTable->where("type=1")->order(" ordernum asc ")->select();
        $this->feetype=$feeTypeData;

        $employeeTable=M("employee");
        //获取业务部门下边的 未锁定的员工
        $employeeData=$employeeTable->where(" department=8 and active=1")->select();
        $this->employee=$employeeData;

        $id=$_GET["id"];
        if($id!='')   //如果编辑的话
        {
            $this->action="edit";
            $data=M("finance");
            $data_Item=$data->where("id=$id")->find();
            $this->finance=$data_Item;

            $agreementtable=M("agreement");
            $agreementid=$data_Item["agreementid"];
            $agreementdata=$agreementtable->where("id=$agreementid")->find();
            $this->agreementdata=$agreementdata;
        }
        else
        {
            $this->action="add";
        }
        $this->display();
    }

    public function saveincome()
    {
        $table = D("finance"); // 实例化模型类
        $action=I('post.action');

        $data = $table->create();
        if(!$data)
        {
            $this->error($table->getError());
        }

        $data["type"]=1;
        $cname=M("employee")->field("name")->find($data["cid"]);
        $data["cname"]=$cname["name"];

        if(empty($data["ordernumber"]))
        {
            $this->error("订单不能为空");
        }

        if($action=="add")
        {
            if (!$table->add($data))
            {
                $this->error("提交失败");
            }
        }
        else
        {
            $result=$table->save($data);
            if(false===$result)
            {
                $this->error("提交失败");
            }
        }
        $this->assign("jumpUrl", U("finance/income"));
        $this->success("操作成功");
    }

    public function addexpend()
    {
        $feeTypeTable=M("feetype");
        $feeTypeData=$feeTypeTable->where("type=2")->order(" ordernum asc ")->select();
        $this->feetype=$feeTypeData;

        $employeeTable=M("employee");
        //获取 未锁定的员工
        $employeeData=$employeeTable->where(" active=1")->select();
        $this->employee=$employeeData;

        $id=$_GET["id"];
        if($id!='')   //如果编辑的话
        {
            $this->action="edit";
            $data=M("finance");
            $data_Item=$data->where("id=$id")->find();
            $this->finance=$data_Item;
        }
        else
        {
            $this->action="add";
        }
        $this->display();
    }

    public function saveexpend()
    {
        $table = D("finance"); // 实例化模型类
        $action=I('post.action');

        $data = $table->create();


        if(!$data)
        {
            $this->error($table->getError());
        }

        if(empty($data["feename"]))
        {
            $this->error("费用类型不得为空");
        }



        $data["type"]=2;
        $cname=M("employee")->field("name")->find($data["cid"]);
        $data["cname"]=$cname["name"];

        if($action=="add")
        {
            if (!$table->add($data))
            {
                $this->error("提交失败");
            }
        }
        else
        {
            $result=$table->save($data);
            if(false===$result)
            {
                $this->error("提交失败");
            }
        }
        $this->assign("jumpUrl", U("finance/expend"));
        $this->success("操作成功");

    }

    public function delfinance()
    {
        $finance = M("finance");
        if (!$finance->delete($_GET['id']))
        {
            $this->error("删除失败");
        } else
        {
            $this->success("删除成功");
        }
    }

    public function feetypeLists()
    {
        $feetypetable=M("feetype");
        $data = $feetypetable->order(" ordernum asc")->select();
        $this->assign('feetypeList', $data); // 数据列表变量赋值
        $this->display(); // 输出模板
    }

    public function addfeetype()
    {
        $id=$_GET["id"];
        if($id!='')   //如果编辑的话
        {
            $this->action="edit";
            $data=M("feetype");
            $data_Item=$data->where("id=$id")->find();
            $this->feetype=$data_Item;
        }
        else
        {
            $this->action="add";

            $data["ordernum"]=100;
            $this->feetype=$data;
        }
        $this->display();
    }

    public function savefeetype()
    {
        $table = D("feetype"); // 实例化模型类
        $action=I('post.action');

        $data = $table->create();
        if(!$data)
        {
            $this->error($table->getError());
        }

        if($action=="add")
        {
            if (!$table->add($data))
            {
                $this->error("提交失败");
            }
        }
        else
        {
            $result=$table->save($data);
            if(false===$result)
            {
                $this->error("提交失败");
            }
        }
        $this->show('<script type="text/javascript" src="__PUBLIC__/libs/js/jquery.js"></script>
	   <script type="text/javascript">
	   $(function(){
		   top.Dialog.alert("成功提交",function(){
			   window.parent.frmright.location.reload();
			   top.Dialog.close()
	   })})</script>
	   ', 'utf-8');
    }

    public function delfeetype()
    {
        $feetype = M("feetype");
        if (!$feetype->delete($_GET['id']))
        {
            $this->error("删除失败");
        } else
        {
            $this->success("删除成功");
        }

    }

    public function agreements()
    {
        Load('extend');

        $agreement="agreement";
        $page = I("get.p");
        $query_keywords = I("get.query_keywords") ;
        if($query_keywords=="0")
            $query_keywords='';
        if (!mb_check_encoding($query_keywords, 'utf-8')){
            $query_keywords = iconv('gb2312', 'utf-8', $query_keywords);
        }
        $this->query_keywords=$query_keywords;
        $condition["customname"]=  array("exp", "like '%$query_keywords%'");
        $data = $this->getPageData($agreement, $page, $condition);

        $this->assign('agreement_list', $data["list"]); // 数据列表变量赋值
        $this->assign('page_method', $data['pageshow']); //分页列表变量赋值
        $this->display(); // 输出模板
    }

    public function addagreement()
    {
        $feetypeTable=M("feetype");
        $feetypeData=$feetypeTable->where("type=1")->select();
        $this->feetype=$feetypeData;

        $employeeTable=M("employee");
        //获取业务部门下边的 未锁定的员工
        $employeeData=$employeeTable->where(" department=8 and active=1")->select();
        $this->employee=$employeeData;

        $id=$_GET["id"];
        if($id!='')   //如果编辑的话
        {
            $this->action="edit";
            $data=M("agreement");
            $data_Item=$data->where("id=$id")->find();
            $this->agreement=$data_Item;
        }
        else
        {
            $this->action="add";
        }
        $this->display();
    }

    public function saveagreement()
    {
        $table = D("agreement"); // 实例化模型类
        $action=I('post.action');

        $data = $table->create();
        if(!$data)
        {
            $this->error($table->getError());
        }

        $data["type"]=1;  //默认首次签约
        $cname=M("employee")->field("name")->find($data["cid"]);
        $data["cname"]=$cname["name"];

        if(empty($data["orderdate"]))
        {
            $this->error("订单日期不能为空");
        }
        if(empty($data["ordernumber"]))
        {
            $this->error("订单不能为空");
        }

        if($action=="add")
        {
            if (!$table->add($data))
            {
                $this->error("提交失败");
            }
        }
        else
        {
            $result=$table->save($data);
            if(false===$result)
            {
                $this->error("提交失败");
            }
        }
        $this->assign("jumpUrl", U("finance/agreements"));
        $this->success("操作成功");
    }

    public function delagreement()
    {
        $agreement = M("agreement");
        if (!$agreement->delete($_GET['id']))
        {
            $this->error("删除失败");
        } else
        {
            $this->success("删除成功");
        }
    }

}



